const HtmlWebpackPlugin = require("html-webpack-plugin");
const { ModuleFederationPlugin } = require("webpack").container;
// const ExternalTemplateRemotesPlugin = require("external-remotes-plugin");
const path = require("path");

module.exports = {
  entry: "./src/index",
  mode: "development",
  devServer: {
    static: [
      {
        directory: path.join(__dirname, 'public'),
      },
      {
        directory: path.join(__dirname, "dist"),
      }
    ],
    port: 3001,
  },
  output: {
    publicPath: "auto",
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        loader: "babel-loader",
        exclude: /node_modules/,
        options: {
          presets: ["@babel/preset-react"],
        },
      },
    ],
  },
  plugins: [
    new ModuleFederationPlugin({
      name: "federation_main",
      remotes: {
        // app1: "app1@./app1.js",
        // app1: `promise new Promise(resolve => {
        //   const remoteUrlWithVersion = 'http://localhost:3001/app1.js'
        //   const script = document.createElement('script')
        //   script.src = remoteUrlWithVersion
        //   script.onload = () => {
        //     const proxy = {
        //       get: (request) => window.app1.get(request),
        //       init: (arg) => {
        //         try {
        //           return window.app1.init(arg)
        //         } catch(e) {
        //           console.log('remote container already initialized')
        //         }
        //       }
        //     }
        //     resolve(proxy)
        //   }
        //   document.head.appendChild(script);
        // })
        // `,
      },
      // shared: { react: { singleton: true }, "react-dom": { singleton: true } },
    }),
    // new ExternalTemplateRemotesPlugin(),
    new HtmlWebpackPlugin({
      template: "./public/index.html",
    }),
  ],
};

